from app.aoduo_admin.models import AdminOperationLog
from app.outdoor.models import Activity
from django.http import JsonResponse
from rest_framework.views import APIView
from common.dictInfo import model_to_dict
from common.authCheck.adminAuth import check_admin_login, adminAuthCheck, getAdminUser
from common.paginator import paginator


class AdminActivityListInfoView(APIView):
    INCLUDE_FIELDS = [
        'name', 'status', 'activity_type',
        'create_time', 'release_time', 'sign_up_start_time',
        'sponsor'
    ]
    # EXCLUDE_FIELDS = [
    #     'participants', 'leader', 'mission'
    # ]

    @check_admin_login
    def get(self, request):
        """
        获取活动列表
        :param request:
        :return:
        """
        admin_user = getAdminUser(request.session.get('admin'))
        authID = 7
        if adminAuthCheck(admin_user, authID):
            # 记录操作行为
            AdminOperationLog.objects.create(
                related_admin=admin_user,
                related_auth_type_id=authID,
                other_message='view activity list'
            )
            allActivity = Activity.objects.all()
            page = request.GET.get('page')
            pageActivity = paginator(allActivity, page, 50)
            allActivityDict = [model_to_dict(activity, fields=self.INCLUDE_FIELDS) for activity in pageActivity]
            return JsonResponse({
                'status': True,
                'all_activity': allActivityDict,
                'has_previous': pageActivity.has_previous(),
                'has_next': pageActivity.has_next()
            })
        return JsonResponse({
            'status': False,
            'errMsg': '你没有权限操作'
        }, status=401)
